package com.zhangtai.modules.controller.member;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.controller.admin.JobController;
import com.zhangtai.modules.controller.aouthLogin.MemberLoginController;
import com.zhangtai.modules.dao.ContractRecordDao;
import com.zhangtai.modules.dao.ProtocolPersonnelDao;
import com.zhangtai.modules.dto.ContractRecordDto;
import com.zhangtai.modules.dto.ProtocolDto;
import com.zhangtai.modules.service.common.ContractUrlService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import com.zhangtai.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@Api(tags="合同管理员工自助")
@RequestMapping("/member")
@Slf4j
public class ContractMemberController {

    @Autowired
    private MemberLoginController memberLoginController;

    @Autowired
    private ContractUrlService contractUrlService;

    @Autowired
    private ProtocolPersonnelDao protocolPersonnelDao;
    @Autowired
    private JobController jobController;
    @Autowired
    private ContractRecordDao contractRecordDao;
//    /**
//     * 获取个人合同列表
//     * @param request
//     * @return
//     * @throws Exception
//     */
//    @GetMapping("/getContractList")
//    @ApiOperation("获取个人合同列表")
//    public R<PageResult<ContractUrlEntity>> getContractList(HttpServletRequest request)throws  Exception{
//        JSONObject data = memberLoginController.getUserInfo(request).getData();
//        Long oid = data.getLong("id"); //只能查询自己的信息
//        PageResult<ContractUrlEntity> urlEntity =  contractUrlService.getPersonContractList(oid,1,10);
//        return R.ok(urlEntity);
//    }

    /**
     * 获取个人合同列表
     * @param request
     * @return
     * @throws Exception
     */
    @GetMapping("/getContractList")
    @ApiOperation("获取个人合同列表")
    public R<PageResult<ContractRecordDto>> getContractList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest request)throws  Exception{
        JSONObject data = memberLoginController.getUserInfo(request).getData();
        Long oid = data.getLong("id"); //只能查询自己的信息
        Page<ContractRecordDto> page=new Page<>(pageNum,pageSize);
        List<ContractRecordDto> contractTerminateDtoList=contractRecordDao.getPersonRecordList(oid,page);
        page.setRecords(contractTerminateDtoList);

        return R.ok(new PageResult<ContractRecordDto>(page));
    }


    @GetMapping("/getProtocolList")
    @ApiOperation("获取个人协议信息列表")
    public R getProtocolList(@RequestParam(value = "projectName",required = false) String projectName,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                             HttpServletRequest request)throws  Exception{
        JSONObject data = memberLoginController.getUserInfo(request).getData();
        Long oid = data.getLong("id"); //只能查询自己的信息

        Page<ProtocolDto> page = new Page<ProtocolDto>(pageNum,pageSize);
        List<ProtocolDto>  protocolDtoList=  protocolPersonnelDao.getProtocolPersonnelListByOid(oid,projectName,page);
        for (ProtocolDto protocolDto:protocolDtoList){
            if (protocolDto.getJobId() != null && StringUtils.isEmpty(protocolDto.getJobname()))
                protocolDto.setJobname(jobController.getJobName(protocolDto.getJobId()));
            if (protocolDto.getOrgId() != null && StringUtils.isEmpty(protocolDto.getOrgname()))
                protocolDto.setOrgname(jobController.getOrgNameById(protocolDto.getOrgId()));
            if (protocolDto.getUnitId() != null && StringUtils.isEmpty(protocolDto.getUnitname()))
                protocolDto.setUnitname(jobController.getOrgNameById(protocolDto.getUnitId()));
        }
        page.setRecords(protocolDtoList);
        return  R.ok(new PageResult<ProtocolDto>(page));
    }


}
